<?php

namespace App\Http\Controllers\Contract;

use App\Helpers\ContractApiTrait;
use App\Helpers\ResponseData;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\Contract\CarContract;
use Modules\Car\Entities\CarOrder;
use Illuminate\Support\Facades\Http;

class FlowController extends Controller
{
    use ResponseData, ContractApiTrait;

    //
    public function flowDetailInfo(Request $request){
        if(empty($request->flow_id)){
            return $this->failed('合同id不能为空');
        }
        $contract=CarContract::where('flow_id',$request->flow_id)->first();
        if(empty($contract)){
            return $this->failed('合同id错误，合同不存在');
        }
        $data=[
            'flowIds'=>[$request->flow_id],
            'agent'=>$this->getAgent(),
        ];
        if($request->org_openid && $request->operator_openid){
            $data['agent']=$this->getAgent($request->org_openid,$request->operator_openid);
        }
        $requestData=$this->getRequestData('DescribeFlowDetailInfo',$data);
        $response=Http::post($this->getQizongApiUrl(),$requestData);
        $resData=$response->json();
        if(isset($resData['code']) && $resData['code']==0){
            $this->updateContractLog($request->flow_id,$resData['data']['flowInfo'][0]);
            return $this->success($resData);
        }
        $this->createContractErrorLog('查询合同失败',$resData,$request);
        return $this->failed($resData['msg']);
    }
    //
    public function updateContractLog($flowId,$contractData)
    {
        CarContract::where('flow_id',$flowId)->update([
            'flow_status'=>$contractData['flowStatus'],
        ]);
    }

    //
    public function getFlowInfoByOrder(Request $request)
    {
        if(empty($request->order_id)){
            return $this->failed('订单id不能为空');
        }
        $flows=CarContract::where('order_id',$request->order_id)->get();
        return $this->success($flows);
    }

    //
    public function getSignUrl(Request $request){
        if(empty($request->flow_id)){
            return $this->failed('合同id不能为空');
        }
        $contract=CarContract::where('flow_id',$request->flow_id)->first();
        if(empty($contract)){
            return $this->failed('合同id错误，合同不存在');
        }
        $data=[
            'flowIds'=>[$request->flow_id],
            'agent'=>$this->getAgent(),
            // 'endpoint'=>'WEIXINAPP',
            'endpoint'=>'APP',
            'autoJumpBack'=>'true',
        ];
        if($request->org_openid && $request->operator_openid){
            $data['agent']=$this->getAgent($request->org_openid,$request->operator_openid);
        }
        $requestData=$this->getRequestData('CreateSignUrls',$data);
        $response=Http::post($this->getQizongApiUrl(),$requestData);
        $resData=$response->json();
        if(isset($resData['code']) && $resData['code']==0){
            return $this->success($resData);
        }
        $this->createContractErrorLog('获取合同签署链接失败',$resData,$request);
        return $this->failed($resData['msg']);
    }

    //
    public function getDownloadUrl(Request $request){
        if(empty($request->flow_id)){
            return $this->failed('合同id不能为空');
        }
        $contract=CarContract::where('flow_id',$request->flow_id)->first();
        if(empty($contract)){
            return $this->failed('合同id错误，合同不存在');
        }
        $data=[
            'flowIds'=>[$request->flow_id],
            'agent'=>$this->getAgent(),
        ];
        if($request->org_openid && $request->operator_openid){
            $data['agent']=$this->getAgent($request->org_openid,$request->operator_openid);
        }
        $requestData=$this->getRequestData('DescribeResourceUrlsByFlows',$data);
        $response=Http::post($this->getQizongApiUrl(),$requestData);
        $resData=$response->json();
        if(isset($resData['code']) && $resData['code']==0){
            return $this->success($resData);
        }
        $this->createContractErrorLog('获取合同下载链接失败',$resData,$request);
        return $this->failed($resData['msg']);
    }
    //
    public function cancelFlow(Request $request){
        if(empty($request->flow_id)){
            return $this->failed('合同id不能为空');
        }
        $contract=CarContract::where('flow_id',$request->flow_id)->first();
        if(empty($contract)){
            return $this->failed('合同id错误，合同不存在');
        }
        $data=[
            'flowId'=>$request->flow_id,
            'agent'=>$this->getAgent(),
        ];
        if($request->cancel_message){
            $data['cancelMessage']=$request->cancel_message;
        }
        if($request->org_openid && $request->operator_openid){
            $data['agent']=$this->getAgent($request->org_openid,$request->operator_openid);
        }
        $requestData=$this->getRequestData('ChannelCancelFlow',$data);
        $response=Http::post($this->getQizongApiUrl(),$requestData);
        $resData=$response->json();
        if(isset($resData['code']) && $resData['code']==0){
            // 合同撤销后软删除本地合同数据 - 改为回调里删除
            // $contract->deleted_at = now();
            // $contract->save();
            return $this->success($resData);
        }
        $this->createContractErrorLog('撤销合同失败',$resData,$request);
        return $this->failed($resData['msg']);
    }
    //
    public function deleteFlow(Request $request){
        if(empty($request->flow_id)){
            return $this->failed('合同id不能为空');
        }
        $contract=CarContract::where('flow_id',$request->flow_id)->first();
        if(empty($contract)){
            return $this->failed('合同id错误，合同不存在');
        }
        $contract->deleted_at = now();
        $contract->save();
        return $this->success('删除成功');
    }

    //
    public function embedWebUrl(Request $request){
        if(empty($request->flow_id)){
            return $this->failed('合同id不能为空');
        }
        $contract=CarContract::where('flow_id',$request->flow_id)->first();
        if(empty($contract)){
            return $this->failed('合同id错误，合同不存在');
        }
        if(empty($request->embed_type)){
            return $this->failed('嵌入页面类型不能为空');
        }
        $data=[
            'businessIds'=>[$request->flow_id],
            'embedType'=>$request->embed_type,
            'agent'=>$this->getAgent(),
        ];
        if($request->org_openid && $request->operator_openid){
            $data['agent']=$this->getAgent($request->org_openid,$request->operator_openid);
        }
        $requestData=$this->getRequestData('ChannelCreateEmbedWebUrl',$data);
        $response=Http::post($this->getQizongApiUrl(),$requestData);
        $resData=$response->json();
        if(isset($resData['code']) && $resData['code']==0){
            return $this->success($resData);
        }
        $this->createContractErrorLog('获取web_url失败',$resData,$request);
        return $this->failed($resData['msg']);
    }


}
